package HashTable;

import java.util.Arrays;

public class _389_FindtheDifference {

    //solution 1:sort the string s and t,then compare the length of two string and get the added letter.
    public char findTheDifference(String s, String t) {
        char[] sc = s.toCharArray();
        char[] tc = t.toCharArray();
        Arrays.sort(sc);
        Arrays.sort(tc);
        for(int i=0;i<sc.length;i++) {
            if (sc[i] != tc[i]) {
                    return tc[i];
            }
        }
        return tc[tc.length - 1];//if the add leeter is the last one.
    }

    //reference solution:use character's number format and get the added letter's number,then return it.
    public char findTheDifference1(String s, String t) {
        char[] sc = s.toCharArray();
        char[] tc = t.toCharArray();
        int sum = 0;
        for(int i=0;i<sc.length;i++) {
            sum += tc[i] - sc[i];
        }
        sum += tc[tc.length - 1];
        return (char)sum;
    }
}
